package com.demo.applicationlistener;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.ServletRequestHandledEvent;

import com.demo.applicationlistener.eventlistener.myevent.HaloEvent;
import com.demo.livemanagement.domain.LiveInformation;
import com.demo.livemanagement.service.LiveService;
import com.demo.livemanagement.timertask.LiveQueue;
import com.demo.utils.applicationcontext.SpringContextUtil;

//注册监听器
@Component
public class WebApplicationStartedEventListener implements ApplicationListener<ApplicationEvent> {

	// private LiveService liveService = (LiveService)
	// BeanTools.getBean(LiveService.class);
	private LiveQueue<LiveInformation> qu = LiveQueue.getInstance();

	// 日志记录
	private Logger log = Logger.getLogger(WebApplicationStartedEventListener.class);

	@Override
	public void onApplicationEvent(ApplicationEvent event) {

		log.info("WebApplicationStartedEventListener:" + event.toString());

		// websocket事件
		// SessionConnectEvent websoket session监控
		// SessionSubscribeEvent websoket session订阅

		// web事件
		// ContextClosedEvent
		// ContextRefreshedEvent
		// ContextStartedEvent
		// ContextStoppedEvent

		// 在这里可以监听到Spring Boot的生命周期
		if (event instanceof ApplicationEnvironmentPreparedEvent) {
			// 初始化环境变量
			log.info("初始化环境变量");
		} else if (event instanceof ApplicationStartingEvent) {
			log.info("正在启动");
		} else if (event instanceof ApplicationPreparedEvent) {
			// 初始化完成
			log.info("初始化完成");
		} else if (event instanceof ApplicationReadyEvent) {
			// 应用已启动完成
			log.info("应用已启动完成");
			LiveService liveService = (LiveService) SpringContextUtil.getBean(LiveService.class);
			List<LiveInformation> list = liveService.selectWaitForUpdateLive();
			// for (LiveInformation liveInformation : list) {
			// System.err.println(liveInformation);
			// }
			qu.addAll(list);

		} else if (event instanceof ContextStartedEvent) {
			// 应用启动，需要在代码动态添加监听器才可捕获
			log.info("应用启动，需要在代码动态添加监听器才可捕获");
		} else if (event instanceof ContextStoppedEvent) {
			// 应用停止
			log.info("应用停止");
		} else if (event instanceof ContextClosedEvent) {
			// 应用关闭
			log.info("应用关闭");
		} else if (event instanceof ContextRefreshedEvent) {
			// 应用刷新
			log.info("应用刷新");
		} else if (event instanceof ServletRequestHandledEvent) {
			// ServletRequestHandledEvent web请求监听
			log.info("web请求监听");
			// log.info("web请求监听:" + event.toString());
		} else if (event instanceof HaloEvent) {
			log.info("HaloEvent");
			event = (HaloEvent) event;
			// HttpServletRequest request = ((HaloEvent) event).getRequest();
			// String attribute = request.getParameter("who");
			// System.err.println(attribute);
		} else {
			log.info("--其他事件--");
		}

	}

}